/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import javax.swing.JTextField;
import javax.swing.JTextPane;
import socof.wget.SOCOFWget;

/**
 *
 * @author Carlos Pinheiro
 */
public class SystemObserver extends Thread
{

    private static SystemObserver uu = new SystemObserver();
    private boolean monitorProcess = true;
    private JTextField numberDownloadersTxt;
    private JTextField numberParsersTxt;
    private JTextPane urlsDownloaded;
    private JTextPane urlsParsed;

    public static SystemObserver getInstance()
    {
        return uu;
    }

    public void setFields(JTextField numberDownloadersTxt, JTextField numberParsersTxt,
            JTextPane urlsDownloaded, JTextPane urlsParsed)
    {
        this.numberDownloadersTxt = numberDownloadersTxt;
        this.numberParsersTxt = numberParsersTxt;
        this.urlsDownloaded = urlsDownloaded;
        this.urlsParsed = urlsParsed;
    }

    @Override
    public void run()
    {
        try
        {

            int executingDownloaders;
            int executingParsers;
            while (monitorProcess && !isInterrupted())
            {
                executingDownloaders = SOCOFWget.getNumberExecutingDownloaders();
                executingParsers = SOCOFWget.getNumberExecutingParsers();

                StringBuilder sb = new StringBuilder();
                for (String s : SOCOFWget.getUrlsDownloaded())
                {
                    sb.append(s);
                    sb.append("\n");
                }

                StringBuilder sb1 = new StringBuilder();
                for (String s : SOCOFWget.getFilesParsed())
                {
                    sb1.append(s);
                    sb1.append("\n");
                }

                urlsDownloaded.setText(sb.toString());
                urlsParsed.setText(sb1.toString());


                numberDownloadersTxt.setText("" + executingDownloaders);
                numberParsersTxt.setText("" + executingParsers);

                Thread.sleep(100);
            }
        }
        catch (InterruptedException ex)
        {
            //Logger.getLogger(Grafica.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
